<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

// This template provides three output formats:

// 1.) it is the data output for Flash Chart with season statistic of a league
// 2.) it's a usual joomla content page to display season statistic of a league

// check for first output option
if ($jl_func == "showstats_getchartdata")
{
  // Calculate Values for Chart Object
  $homeSum = array();
  $awaySum = array();
  $matchDayGoalsCount = array();
  $label = '';
  $acYLabel = 1;
  
  $matchDayGoalsCountMax = 0;
  foreach( $rows as $rw )
  {
    // check, if both results are missing and avoid drawing the flatline of "0" goals for not played games yet
    if ((!$rw->homegoalspd) && (!$rw->guestgoalspd)) $NoSumAvail = true;
    if (!$rw->homegoalspd) $rw->homegoalspd = 0;
    if (!$rw->guestgoalspd) $rw->guestgoalspd = 0;
    $homeSum[] = $rw->homegoalspd;
    $awaySum[] = $rw->guestgoalspd;
    if ($NoSumAvail)
    {
      $matchDayGoalsCount[] = 'null';
    }
    else
    {
      $matchDayGoalsCount[] = $rw->homegoalspd + $rw->guestgoalspd;
      if ($matchDayGoalsCountMax < ($rw->homegoalspd + $rw->guestgoalspd)) $matchDayGoalsCountMax = ($rw->homegoalspd + $rw->guestgoalspd);
    }
    if ($acYLabel > 1) $label .= ',';
    $label .= $acYLabel;
    $acYLabel++;
  }

  // Generate RenderCommands for Chart Object

  include( $mosConfig_absolute_path.'/components/com_joomleague/lib/open-flash-chart/open-flash-chart.php' );
  $g = new graph();
  // backgound color of flash statistic
  $g->bg_colour = $overall_config['bg_colour'];
  // inner background color with gradient
  $g->set_inner_background( $overall_config['inner_bg_gradient_1'], $overall_config['inner_bg_gradient_2'], $overall_config['inner_bg_gradient_orientation'] );
  // data 1
  $g->set_data( $homeSum );
  // first bar
  $g->bar( 50, $overall_config['bar1'], _JL_STATS_HOME, 10 );
  // data 2
  $g->set_data( $awaySum );
  $g->bar( 50, $overall_config['bar2'], _JL_STATS_AWAY, 10 );
  // data 3
  $g->set_data( $matchDayGoalsCount );
  $g->line_dot( 3, 5, $overall_config['bar3'], _JL_STATS_TOTAL, 10 );    // <-- 3px thick + dots

  $g->set_x_labels( array( $label ) );
  // well, general formula: $givenmaxval - $givenmaxval % $wishedcountofsteps + $wishedcountofsteps
  // if you want no space to top if number is divisibly by $wishedcountofsteps you must not add
  // $wishedcountofsteps ... like:
  // $myset_y_max = (($givenmaxval % $wishedcountofsteps)==0) ? $givenmaxval - $givenmaxval % $wishedcountofsteps : $givenmaxval - $givenmaxval % $wishedcountofsteps + $wishedcountofsteps;
  // $g->set_y_max( $myset_y_max );
  // manygreetz, carsten :-)
  $g->set_y_max( $matchDayGoalsCountMax + 5 - $matchDayGoalsCountMax % 5 );
  $g->y_label_steps( 5 );
  // x-axis color (color for bottom x-axis, color for inner x-axis)
  $g->x_axis_colour( $overall_config['x_axis_colour'], $overall_config['y_axis_colour_inner'] );
  // y-axis color (color for left y-axis, color for inner y-axis)
  $g->y_axis_colour( $overall_config['y_axis_colour'], $overall_config['x_axis_colour_inner'] );
  $g->set_tool_tip( _JL_STATS_GOALS.': #val#' );  
  echo  $g->render(); // Method doesn't really render, but it returns a drawlist as multiline text string
  exit();
}

// ELSE (second output option):

  // Show Content of ShowStats Template
?>

<!-- Header with Project Name-->
<?php if ($showStats_config['show_project_heading'] == "1" OR $overall_config['show_project_heading'] == "1") { ?>
<div class="componentheading" <?php if ($showStats_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") echo 'style="float:left;"';?>><?php echo HTML_joomleague::showTitle($project, $overall_config['project_heading_type']); ?></div>
<!-- Druckfunktion -->
<?php if ($showStats_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") {
HTML_joomleague::PrintIcon( $row, $params, false, '' );
};?>
<!-- Druckfunktion Ende -->
<?php } ?>
<!-- Header END -->

<!-- sectionheader -->
<table width="100%" class="contentpaneopen">
  <tr>
    <td class="contentheading">
      <?php echo _JL_STATS;?>
    </td>
  </tr>
</table>
<!-- sectionheader ends -->
<?php if ($showStats_config['show_general_stats'] == "1" OR $showStats_config['show_goals_stats'] == "1") { ;?>
<br />

<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
  <tr>
<?php if ($showStats_config['show_general_stats'] == "1") { ;?>  
    <td class="sectiontableheader" width="50%">
      <?php echo _JL_STATS_GENERAL;?>
    </td>
<?php } ;?>
<?php if ($showStats_config['show_goals_stats'] == "1") { ;?>
    <td class="sectiontableheader" >
      <?php echo _JL_STATS_GOALS;?>
    </td>
<?php } ;?>    
  </tr>
  <tr>
<?php if ($showStats_config['show_general_stats'] == "1") { ;?>  
  <td>
    <table>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_MATCHDAYS;?>:</td><td width="10%">&nbsp;</td><td align="right"> <?=$totalrounds;?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_CURRENT_MATCHDAY;?>:</td><td width="10%">&nbsp;</td><td align="right"> <?=$actualround;?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_MATCHES ." / ". _JL_TABLE_MD_SHORT;?>:</td><td width="10%">&nbsp;</td><td align="right"> <?= round (($totals[0]->totalmatches / $totalrounds),2);?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_MATCHES_OVERALL;?>:</td><td width="10%">&nbsp;</td><td align="right"><?=$totals[0]->totalmatches;?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_MATCHES_PLAYED;?>:</td><td width="10%">&nbsp;</td><td align="right"> <?=$totals[0]->playedmatches;?></td></tr>
      <tr><td colspan="2"><b><?php echo _JL_STATS_MATCHES_HIGHEST_WON_HOME;?>:</b></td></tr><tr><td nowrap="nowrap"> <?=$temphighesthome['team1']." - ".$temphighesthome['team2']. "</td><td width='10%'>&nbsp;</td><td nowrap=\"nowrap\" align=\"right\">".$temphighesthome['e1']." : ".$temphighesthome['e2'];?></td></tr>
      <tr><td colspan="2"><b><?php echo _JL_STATS_MATCHES_HIGHEST_WON_AWAY;?>:</b></td></tr><tr><td nowrap="nowrap"> <?=$templosthighesthome['team1']." - ".$templosthighesthome['team2']. "</td><td width='10%'>&nbsp;</td><td nowrap=\"nowrap\" align=\"right\">".$templosthighesthome['e1']." : ".$templosthighesthome['e2'];?></td></tr>
    </table>
  </td>
<?php } ;?>
<?php if ($showStats_config['show_goals_stats'] == "1") { ;?>
  <td>
    <table>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_TOTAL;?>:</td><td width="10%">&nbsp;</td><td align="right"> <?=$totals[0]->sumgoals;?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_TOTAL_PER_MATCHDAY;?>:</td><td width="10%">&nbsp;</td><td align="right"> <? if ($totalrounds>0) echo round (($totals[0]->sumgoals / $totalrounds),2); else echo '0';?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_TOTAL_PER_MATCH;?>:</td><td width="10%">&nbsp;</td><td align="right"> <? if ($totals[0]->playedmatches>0) echo round (($totals[0]->sumgoals / $totals[0]->playedmatches),2); else echo '0'; ?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_HOME;?></td><td width="10%">&nbsp;</td><td align="right"> <?=$totals[0]->homegoals;?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_HOME_PER_MATCHDAY;?>:</td><td width="10%">&nbsp;</td><td align="right"> <? if ($totalrounds>0) echo round(($totals[0]->homegoals / $totalrounds),2); else echo '0';?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_HOME_PER_MATCH;?>:</td><td width="10%">&nbsp;</td><td align="right"> <? if ($totals[0]->playedmatches>0) echo round(($totals[0]->homegoals / $totals[0]->playedmatches),2); else echo '0'; ?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_AWAY;?></td><td width="10%">&nbsp;</td><td align="right"> <?=$totals[0]->guestgoals;?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_AWAY_PER_MATCHDAY;?>:</td><td width="10%">&nbsp;</td><td align="right"> <? if ($totalrounds>0) echo round(($totals[0]->guestgoals / $totalrounds),2); else echo '0'; ?></td></tr>
      <tr><td nowrap="nowrap"><?php echo _JL_STATS_GOALS_AWAY_PER_MATCH;?>:</td><td width="10%">&nbsp;</td><td align="right"> <? if ($totals[0]->playedmatches>0) echo round(($totals[0]->guestgoals / $totals[0]->playedmatches),2); else echo '0';?></td></tr>
    </table>
  </td>
<?php } ;?>  
</tr>
</table>

<br />
<?php } ;?>

<?php if ($showStats_config['show_goals_stats_flash'] == "1") { ;?>
<!-- Flash Statistik Start -->
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
    <td class="sectiontableheader"><?php echo _JL_STATS_GOALS_STATISTIC;?></td>
  </tr>
  <tr><td></td></tr>
  <tr>
    <td align="center">
      <?php open_flash_chart_object( '100%', 250, $mosConfig_live_site."/index.php?option=com_joomleague%26func=showStats_GetChartData%26p=".$p."%26Itemid=".$Itemid );?>
    </td>  
  </tr>
</table>
<!-- Flash Statistik END -->
<br />
<?php } ;?>
<?php if ($showStats_config['show_events_stats'] == "1") { ;?>
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
    <td class="sectiontableheader"><?php  echo _JL_MODULE_NEXT_MATCH_STATISTIC." &nbsp;";
     if (isset($eventtype) && $eventtype>0) { echo $eventtypes[$eventtype]->name; } else echo _JL_ADMIN_EDIT_FORM_MATCHEVENTS_MATCHEVENTS;?>
   </td>
  </tr>
  <tr>
    <td><?php HTML_joomleague::show_event_stats ($eventtypes, $eventrankings, $events_config);?></td>  
  </tr>
</table>

<br />
<?php } ;?>


<!-- Print Button START -->
<?php if ($showStats_config['show_back_button'] > 0 OR $overall_config['show_back_button'] > 0) {;?>
<?php if ($showStats_config['show_back_button'] == "1" OR $overall_config['show_back_button'] == "1") echo '<div align="left">'; else echo '<div align="right">';?>
<?php
$params = & new mosParameters( '' );
$params->def( 'back_button', true );
$params->def( 'popup', false );
mosHTML::BackButton( $params );
?></div> 
<?php };?>
<!-- Print Button END -->
